namespace FIREFLYX {
    export class InputManager {
        private static _element: Nullable<HTMLElement>;
        private static _listeners: [any, any][] = [];
        private static _isInited = false;
        public static init() {
            if (this._isInited) {
                return;
            }
            MouseManager.instance.init(this._listeners);
            KeyboardManager.instance.init(this._listeners);
            TouchManager.instance.init(this._listeners);
            PointManager.instance.init();

            const canvas = App.canvasElement;
            this.attach(canvas);
            canvas.focus();
            canvas.addEventListener(`contextmenu`, (event) => event.preventDefault());  //关闭canvas 的右键菜单

            this._isInited = true;
        }

        private static attach(element: HTMLElement) {
            if (this._element) {
                this.detach();
            }
            this._element = element;
            this._listeners.forEach((listener) => {
                if (listener && element) {
                    element.addEventListener(listener[0], listener[1], true);  //reg
                }
            });
        }

        private static detach() {
            let element = this._element;
            if (!element) {
                return;
            }
            this._listeners.forEach((listener) => {
                if (listener && element) {
                    element.removeEventListener(listener[0], listener[1], false);  //unreg
                }
            });
            this._element = null;
        }
    }

}